package com.testDemo.design.chain;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class ChainTest {

    public static void main(String[] args) {
        List<Filter> lists = new ArrayList<>();
        lists.add(new SayWorldFilter());
        lists.add(new SayHelloFilter());

        Request request = (Request) (RequestWrap.wrap(new MyRequest(), new SayWorldFilter()));


        request = (Request) (RequestWrap.wrap(request, new SayHelloFilter()));

        request.test();
    }

    // 责任链模式，用链表实现。
    @Test
    public void test() {
        List<Filter> lists = new ArrayList<>();
        lists.add(new SayWorldFilter());
        lists.add(new SayHelloFilter());

        Request request = new MyRequest();

        for (Filter filter : lists) {
            Request next = request;
            request = new Request() {
                @Override
                public String test() {
                    return filter.filter(next);
                }
            };
        }
        request.test();
    }
}
